package base.C00_Recursion;

import org.testng.annotations.Test;

/**
 * 解决堆栈溢出
 * 公式： f（n） = f（n-1）+f（n-2）
 */
public class RecursionQuestion {
    public static int depth = 0;
    //限制堆栈深度为100
    public static int sum(int num) throws Exception {
        depth++;
        if(num==1)
            return 1;
        if (num == 2) {
            return 2;
        }
        if(depth==100)
            throw new Exception("StackOverFlowException: depth is over 100");

        return sum(num-1)+sum(num-2);
    }

    @Test
    public void testDepth() {
        try {
            System.out.println(sum(99));
            System.out.println(sum(10));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
